from google.appengine.ext import db
import time
import datetime

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

print 'Content-Type: text/plain'
print ''
print 'Database Demo: %s' % time.asctime()


class Student(db.Model):
  name = db.StringProperty(required=True)
  home_state = db.StringProperty(required=True)
  birthday = db.StringProperty(required=False)
  ts = db.DateTimeProperty(auto_now_add=True)

def CreateStudent(name, home_state, birthday=None):
  print 'Creating student: %s' %(name)
  student = Student(name=name, home_state=home_state, birthday=birthday)
  student.put()

def GetAllStudents():
  return Student.all()
  
def PrintStudents(query):
  print 'Name\t\tHome\t\tBirthday\t\tDate_added'
  for r in query:
    print '%s\t\t%s\t\t%s\t\t%s' %(r.name, r.home_state, r.birthday, r.ts)


def PrintAllStudents():
  print 'Listing all students in the Datastore'
  query = GetAllStudents()
  PrintStudents(query)

def FindStudentsInState(state):
  query = Student.all()
  query.filter("home_state =", state)
  return query

def ResetStudents():
  print 'Resetting Students'
  query = Student.all()
  if query:
    for r in query:
      r.delete()
  print ''

def MoveStudents(from_state, to_state):
  print 'Moving all students from: %s to %s' %(from_state, to_state)
  query = Student.all()
  query.filter("home_state =", from_state)
  for r in query:
    r.home_state = to_state
    r.put()

def GetNStudents(n=10):
  print 'Getting first %s students' %(n)
  query = Student.all()
  return query.fetch(n)

def GetStudentRange(limit=1, offset=1):
  print 'Getting %d students from position %d' %(limit, offset)
  query = Student.all()
  return query.fetch(limit=limit, offset=offset)

def ChangeBirthdays(from_birthday, to_birthday):
  print 'Changing all birthdays from: %s to %s' %(from_birthday, to_birthday)
  query = Student.all()
  query.filter("birthday =", from_birthday)
  for r in query:
    r.birthday = to_birthday
    r.put()
    
def FindStudentsWithBirthday(birthday):
  print 'Finding all students born on: %s' %(birthday)
  query = Student.all()
  query.filter('birthday =', birthday)
  return query 

class MainHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication([('/.*', MainHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()



print ''
ResetStudents()
CreateStudent('Mike', 'Nevada', '01-01-1980')
CreateStudent('Amy', 'California')
CreateStudent('Larry', 'California')
CreateStudent('Sarah', 'New York')
print ''
PrintAllStudents()
print ''
print 'Now finding students in California'
PrintStudents(FindStudentsInState('California'))
print ''
MoveStudents('California', 'Oregon')
print ''
PrintAllStudents()
print ''
PrintStudents(GetNStudents(2))

print ''
PrintStudents(GetStudentRange(2,1))
print '\n'
ChangeBirthdays(None, str(datetime.datetime.now()))
PrintAllStudents()
print '\n'
PrintStudents(FindStudentsWithBirthday('12-14-1993'))
